<template>
  <div style="height: 100%">
    <div>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item>
          <el-input v-model="searchForm.name" placeholder="日志标题" v-if="sys_log_view" clearable @clear="findByPage"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="findByName" size="medium" v-if="sys_log_view" icon="el-icon-search">查询</el-button>
        </el-form-item>
      </el-form>
      <v-table :columns="columns" :data="tableData" :loading="loadingData" @handleClick="handleClick"
               @findPage="findByPage" @handleBatchDelete="handleBatchDelete" :perms-delete="sys_log_delete"
               @selectionChange="selectionChange" :table-operation="tableOperation">
        <template slot="operation" slot-scope="scope">
          <el-button
            v-if="sys_log_view"
            @click="handleView(scope.data.row)"
            :size="viewOperation.size || 'mini'"
            :style="{color: viewOperation.color|| ''}"
            :icon="viewOperation.icon"
            type="text">
            {{viewOperation.name}}
          </el-button>

          <el-button
            v-if="sys_log_delete"
            @click="handleDelete(scope.data.row)"
            :size="deleteOperation.size || 'mini'"
            :style="{color: deleteOperation.color|| ''}"
            :icon="deleteOperation.icon"
            type="text">
            {{deleteOperation.name}}
          </el-button>
        </template>
      </v-table>
    </div>
  </div>
</template>

<script>
  import vueTable from '../../../components/vueTable.vue'
  import {logApi} from '../../../http/modules/log'
  import VTable from "../../../components/table/table";
  import website from "../../../const/website";
  import {mapGetters} from "vuex";

  export default {
    name: "log",
    components: {
      VTable,
      vueTable
    },
    data() {
      return {
        tableData: {
          totalSize: 10,
          content: []
        },
        pageRequest: {
          pageNum: 1,
          pageSize: 10
        },
        columns: [
          {prop: "type", label: "类型", minWidth: 100},
          {prop: "title", label: "标题", minWidth: 100},
          {prop: "remoteAddr", label: "IP地址", minWidth: 100},
          {prop: "method", label: "请求方式", minWidth: 100},
          {prop: "time", label: "请求时间", minWidth: 100},
          {prop: "createTime", label: "创建时间", minWidth: 100},
          {prop: "createBy", label: "操作人", minWidth: 80}
        ],
        tableOperation: {
          operation: true
        },
        viewOperation: website.viewOperation, // 查看操作
        deleteOperation: website.deleteOperation, // 删除操作
        searchForm: {
          name: '',
          pageNum: 0,
          pageSize: 10
        },
        selections: [],
        loadingData: true
      }
    },
    methods: {
      findByPage(data) {
        this.loadingData = true
        if (data != null) {
          this.pageRequest = data
        }
        if (this.searchForm.name !== '' && this.searchForm.pageNum !== data.pageNum) {
          this.searchForm.pageNum = data.pageNum
          this.findByName()
        } else {
          logApi.findPage(this.pageRequest).then(res => {
            this.tableData = res.data
            this.loadingData = false
          }).catch(error => {
            console.log(error)
            this.$message.error('网络错误')
          })
        }

      },

      handleClick(row, type) {
        let rows = []
        if (type === 'delete') {
          rows.push(row)
          this.handleDelete(rows)
        }
      },
      handleDelete(rows) {
        // 删除用户
        this.$confirm('此操作将删除日志，是否继续？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.delete(rows)
        }).catch(() => {
          this.$message.info('已取消删除')
        })

      },
      delete(datas) {
        let records = []
        datas.forEach(item => {
          records.push({logId: item.logId})
        })
        logApi.delete(records).then(res => {
          this.$message.success('删除成功')
          this.findByPage()
        }).catch(error => {
          this.$message.error('删除失败')
        })
      },
      handleBatchDelete() {
        this.handleDelete(this.selections)
      },
      selectionChange(selections) {
        this.selections = selections
      },
      findByName() {
        this.loadingData = true
        logApi.findByName(this.searchForm).then(res => {
          this.tableData = res.data
          this.loadingData = false
        }).catch(error => {
          console.log(error)
          this.$message.error('网络错误')
        })
      }
    },
    computed: {
      ...mapGetters(["permissions"])
    },
    created() {
      this.sys_log_view = this.permissions["sys:log:view"]
      this.sys_log_add = this.permissions["sys:log:add"]
      this.sys_log_edit = this.permissions["sys:log:edit"]
      this.sys_log_delete = this.permissions["sys:log:delete"]
    },
    mounted() {
      // this.findByPage(this.pageRequest)
    }
  }
</script>

<style scoped>

</style>
